package com.android.equipment.dao.operate;

import com.android.equipment.ObjectBox;
import com.android.equipment.dao.RealTimeAlam;
import com.android.equipment.dao.RealTimeAlam_;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.query.QueryBuilder;

public class DaoRealTimeAlam {
    static Box<RealTimeAlam> realTimeAlamBox;
    private static DaoRealTimeAlam instance;

    private DaoRealTimeAlam() {
    }

    public static DaoRealTimeAlam getInstance() {
        if (instance == null) {
            instance = new DaoRealTimeAlam();
            realTimeAlamBox = ObjectBox.get().boxFor(RealTimeAlam.class);
        }
        return instance;
    }

    public long addRealTimeAlam(RealTimeAlam realTimeAlam) {
        return realTimeAlamBox.put(realTimeAlam);
    }


    public void deleteRealTimeAlam(long id) {
        realTimeAlamBox.remove(id);
    }

    public Long getRealTimeAlamId(long workId,String deviceCode, String node) {

               List<RealTimeAlam> realTimeList = realTimeAlamBox
                .query()
                .equal(RealTimeAlam_.workId, workId)
                .equal(RealTimeAlam_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .equal(RealTimeAlam_.node, node, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .build().find();

               if (!realTimeList.isEmpty()){
                   return realTimeList.get(0).getId();
               }else {
                   return null;
               }

    }

    public List<RealTimeAlam> getRealTimeAlamByWorkIdAndDeviceCode(long workId,String deviceCode){
        return realTimeAlamBox
                .query()
                .equal(RealTimeAlam_.workId, workId)
                .equal(RealTimeAlam_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .build().find();
    }

    public void deleteAllRealTime(){
        realTimeAlamBox.removeAll();
    }
}
